﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using zyconfig;

namespace zymain
{
    public partial class FrmCgydSetting : Form
    {
        public int uid;
        public int gjm;
        private List<int> JfjmList = new List<int>();
        public int jfjm=0;
        public string ddh;
        public FrmCgydSetting()
        {
            InitializeComponent();
        }
        private void AddJfKay()
        {
            using (DbOperHandler _open = new Common().Doh())
            {
                JfjmList.Clear();
                string sql = "select * from 采购经费库 where 支配人代码 like '% " + uid + " %' and 馆键码="+this.gjm+ " and 总额>实付 and 终止年代>= YEAR(GETDATE())  and 起始年代<= YEAR(GETDATE()) and 截止日期>=GETDATE() order by 序号 desc";
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows) {
                        this.jfKay.Items.Add(dRow["经费名称"]);
                        this.JfjmList.Add(Convert.ToInt32(dRow["键码"].ToString().Trim()));
                    }
                    this.jfKay.SelectedIndex = 0;
                    this.jfjm = this.JfjmList[0];
                }
            }
        }

        private void jf_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index =this.jfKay.SelectedIndex;
            this.jfjm = this.JfjmList[index];
        }

        private void FrmCgydSetting_Load(object sender, EventArgs e)
        {
            this.AddJfKay();
            this.ddhTxt.Text = DateTime.Now.ToString("yyyyMMdd");
        }

        private void saneBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.ddhTxt.Text))
                this.ddhTxt.Text = DateTime.Now.ToString("yyyyMMdd");
            this.ddh = this.ddhTxt.Text.Trim();
            bool f = true;
            if(string.IsNullOrEmpty(this.ddh))
            {
                MessageBox.Show("订单号不能为空");
                f = false;
                this.ddhTxt.Focus();
            }
            if (this.jfjm==0)
            {
                MessageBox.Show("无可用的经费！");
                f = false;
            }
            if (f)
            {
                DialogResult = DialogResult.OK;
                base.Close();
            }
            
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            base.Close();
        }

        private void Frmcgs_Closing(object sender, FormClosingEventArgs e)
        {
            base.Close();
        }
    }
}
